package cc.rengu.igas.bsps.core.service.trans;

import cc.rengu.igas.bsps.common.constant.BspsAppParamConstant;
import cc.rengu.igas.bsps.common.dao.*;
import cc.rengu.igas.bsps.common.dao.impl.*;
import cc.rengu.igas.bsps.common.entity.*;
import cc.rengu.igas.bsps.common.enums.*;
import cc.rengu.igas.bsps.core.model.UserSessionInfo;
import cc.rengu.igas.bsps.core.service.base.ManageService;
import cc.rengu.igas.bsps.facade.base.Header;
import cc.rengu.igas.bsps.facade.bean.MchntBindInfoBean;
import cc.rengu.igas.bsps.facade.bean.PrivilegeInfoBean;
import cc.rengu.igas.bsps.facade.bean.UserInfoBean;
import cc.rengu.igas.bsps.facade.request.GetUserInfoListRequest;
import cc.rengu.igas.bsps.facade.response.GetUserInfoListResponse;
import cc.rengu.igas.share.common.util.SensitiveDataUtil;
import cc.rengu.igas.share.core.model.GroupMchntInfo;
import cc.rengu.igas.share.core.realize.MchntService;
import cc.rengu.igas.share.core.realize.impl.MchntServiceImpl;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.entity.SysParam;
import cc.rengu.oltp.service.common.enums.SmsTypeEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.service.realize.SmsService;
import cc.rengu.oltp.service.realize.SysParamService;
import cc.rengu.oltp.service.realize.impl.SmsServiceImpl;
import cc.rengu.oltp.service.realize.impl.SysParamServiceImpl;
import cc.rengu.oltp.utility.util.*;
import com.alibaba.fastjson.JSON;
import org.apache.commons.codec.binary.Base64;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static com.alibaba.fastjson.JSON.toJSONString;

/**
 * 短信验证码登录/手机获取用户列表.
 *
 * @author zhangxuran (联调修改)
 * @version 1.0
 * @date 2020.04.18
 */
public class GetUserInfoListService extends ManageService {

    @Override
    protected Object bizBeforeCust() throws Exception {
        GetUserInfoListRequest getUserInfoListRequest = new GetUserInfoListRequest();
        ConvertUtil.convertOutput(getUserInfoListRequest);
        return getUserInfoListRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        GetUserInfoListRequest getUserInfoListRequest = (GetUserInfoListRequest) request;
        Header header = getUserInfoListRequest.getHeader();
        String instId = getUserInfoListRequest.getHeader().getInstId();
        String mobileNo = getUserInfoListRequest.getMobileNo();
        String smsCode = getUserInfoListRequest.getSmsCode();
        String authId = getUserInfoListRequest.getSmsAuthId();
        String channelId = getUserInfoListRequest.getHeader().getChanlId();
        rglog.info("必填性校验");
        if (StringUtil.isEmptyOrNull(instId) || StringUtil.isEmptyOrNull(mobileNo) || StringUtil.isEmptyOrNull(smsCode) || StringUtil.isEmptyOrNull(authId)) {
            rglog.error("参数必填性校验失败！instId:<{}>,mobileNo:<{}>,smsCode:<{}>,authId:<{}>", instId, mobileNo, smsCode, authId);
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }

        GetUserInfoListResponse getUserInfoListResponse = new GetUserInfoListResponse();
        /*短信验证码登录,验证动态验证码 */
        rglog.info("校验短信验证码开始");
        SmsService smsService = new SmsServiceImpl();
        smsService.verifySmsCode(instId, SmsTypeEnum.USER_LOGIN.getSmsType(), smsCode, authId);
        rglog.info("校验短信验证码完成");
        /* 根据手机号查询用户列表信息 */
        rglog.info("根据手机号查询用户列表");
        BspsUserInfoMapper bspsUserInfoMapper = new BspsUserInfoMapperImpl();
        List<UserInfo> userInfoList = bspsUserInfoMapper.selectBspsUserInfoListByMobileNo(instId, mobileNo);
        if (CollectionUtils.isEmpty(userInfoList)) {
            rglog.error("用户手机号：<{}>userInfoList用户信息不存在!", mobileNo);
            throw new BizException(RespCodeEnum.MOBILE_USER_EXIST_ERROR.getRespCode(), RespCodeEnum.MOBILE_USER_EXIST_ERROR.getRespDesc());
        }
        List<UserInfo> userFilterList = userInfoList.stream().filter(item -> !"LOGINID".equals(item.getUserLoginId())).collect(Collectors.toList());
        if (userInfoList.size() > 1 && CollectionUtils.isEmpty(userFilterList)) {
            rglog.error("用户手机号：<{}>信息不存在，userInfoList非空，userFilterList为空!", mobileNo);
            throw new BizException(RespCodeEnum.MOBILE_USER_EXIST_ERROR.getRespCode(), RespCodeEnum.MOBILE_USER_EXIST_ERROR.getRespDesc());
        } else if (userInfoList.size() == 1 || userFilterList.size() == 1) {
            UserInfo userInfo;
            if (userInfoList.size() == 1) {
                userInfo = userInfoList.get(0);
            } else {
                userInfo = userFilterList.get(0);
            }
            BeanUtil.beanCopy(userInfo, getUserInfoListResponse);
            if (!StringUtil.isEmptyOrNull(userInfo.getMobileNo())) {
                getUserInfoListResponse.setPhoneNo(SensitiveDataUtil.desensitization("1", userInfo.getMobileNo()).split("\\|")[0]);
            }
            /*头像路径赋值*/
            if (!StringUtil.isEmptyOrNull(userInfo.getUserImagePath())) {
                rglog.info("头像路径赋值：" + userInfo.getUserImagePath());
                getUserInfoListResponse.setUserImagePath(Base64.encodeBase64String(userInfo.getUserImagePath().getBytes()));
            }
            /*是否设置交易密码标识赋值*/
            if (StringUtil.isEmptyOrNull(userInfo.getTransPassword())) {
                rglog.info("未设置交易密码");
                getUserInfoListResponse.setVerifyFlag("N");
            } else {
                rglog.info("已设置交易密码");
                getUserInfoListResponse.setVerifyFlag("Y");
            }
            /* 获取用户绑定信息及对应角色权限 */
            BspsMchntBaseInfoMapper bspsMchntBaseInfoMapper = new BspsMchntBaseInfoMapperImpl();
            BspsMchntBindInfoMapper bspsMchntBindInfoMapper = new BspsMchntBindInfoMapperImpl();
            BspsUserRoleInfoMapper bspsUserRoleInfoMapper = new BspsUserRoleInfoMapperImpl();
            rglog.info("查询绑定商户信息");
            List<MchntBindInfo> mchntBindInfoList = bspsMchntBindInfoMapper.selectBspsMchntBindInfoListByUserId(instId, userInfo.getUserId());
            List<MchntBindInfoBean> facadeMchntBindInfoBeanList = new ArrayList<>();
            BspsAgentInfoMapper bspsAgentInfoMapper = new BspsAgentInfoMapperImpl();
            BspsPlatformMchntInfoMapper bspsPlatformMchntInfoMapper = new BspsPlatformMchntInfoMapperImpl();
            if (!CollectionUtils.isEmpty(mchntBindInfoList)) {
                List<MchntBindInfo> mchntBindInfos = mchntBindInfoList.stream().filter(item -> item.getBindMchntType().equals(BspsMchntTypeEnum.GROUP_MCHNT.getMchntType()) || item.getBindMchntType().equals(BspsMchntTypeEnum.AGENT_MCHNT.getMchntType())
                        || item.getBindMchntType().equals(BspsMchntTypeEnum.PLAT_MCHNT.getMchntType())).collect(Collectors.toList());
                if (!CollectionUtils.isEmpty(mchntBindInfos) && !"PCWEB".equals(channelId)) {
                    rglog.error("商户为代理商、平台商户或者集团商户，在微信端登录");
                    throw new BizException(RespCodeEnum.MCHNT_CHANNEL_REGISTER.getRespCode(), RespCodeEnum.MCHNT_CHANNEL_REGISTER.getRespDesc());
                }
                rglog.info("商户绑定关系赋值开始");
                for (MchntBindInfo mchntBindInfo : mchntBindInfoList) {
                    MchntBindInfoBean facadeMchntBinInfo = new MchntBindInfoBean();
                    BeanUtil.beanCopy(mchntBindInfo, facadeMchntBinInfo);

                    rglog.info("用户角色权限查询");
                    List<UserRoleInfo> userRoleInfoList = bspsUserRoleInfoMapper.selectBspsUserRoleInfoToChannelMenu(instId, mchntBindInfo.getBindUserRole(), channelId);
                    if (!CollectionUtils.isEmpty(userRoleInfoList)) {
                        List<PrivilegeInfoBean> privilegeInfoBeanList = userRoleInfoList.stream().map(this::convertPrivilegeInfo).collect(Collectors.toList());
                        facadeMchntBinInfo.setPrivilegeInfoBeanList(privilegeInfoBeanList);
                    }
                    rglog.info("商户类型赋值");
                    facadeMchntBinInfo.setMchntType(mchntBindInfo.getBindMchntType());

                    /*退款权限-法人、店长都有权限,白名单的收银员有权限*/
                    if (((BspsUserRoleTypeEnum.MCHNT_ARTIF.getRoleType().equals(mchntBindInfo.getBindUserRole()))
                            || (BspsUserRoleTypeEnum.MCHNT_MANAGER.getRoleType().equals(mchntBindInfo.getBindUserRole())))) {
                        rglog.info("法人、店长登录");
                        //有退款权限
                        facadeMchntBinInfo.setRefundFlag(AppParamConstant.YES);
                    } else {
                        rglog.info("非法人、店长登录");
                        //校验白名单收银员退款权限,null/NO为无权限
                        SysParamService sysParamService = new SysParamServiceImpl();
                        SysParam sysParam = sysParamService.getSysParamInfo(instId, BspsAppParamConstant.BSPS_REFUND_MERCH, mchntBindInfo.getMchntNo());
                        if (null != sysParam && !StringUtil.isEmptyOrNull(sysParam.getParamValue()) && AppParamConstant.YES.equals(sysParam.getParamValue())) {
                            rglog.info("白名单商户");
                            facadeMchntBinInfo.setRefundFlag(AppParamConstant.YES);
                        } else {
                            rglog.info("非白名单商户");
                            facadeMchntBinInfo.setRefundFlag(AppParamConstant.NO);
                        }
                    }

                    rglog.info("判断商户类型及状态");
                    if (BspsMchntTypeEnum.AGENT_MCHNT.getMchntType().equals(mchntBindInfo.getBindMchntType())) {
                        AgentInfo agentInfo = bspsAgentInfoMapper.selectBspsAgentInfoByInstIdAndAgentCode(instId, mchntBindInfo.getMchntNo());
                        if (null == agentInfo || !MchntStatusEnum.NORMAL.getStatus().equals(agentInfo.getAgentStatus())) {
                            rglog.info("代理商信息查询异常或状态异常");
                        } else {
                            facadeMchntBinInfo.setMchntName(agentInfo.getAgentName());
                            facadeMchntBindInfoBeanList.add(facadeMchntBinInfo);
                        }
                    } else if (BspsMchntTypeEnum.PLAT_MCHNT.getMchntType().equals(mchntBindInfo.getBindMchntType())) {
                        PlatformMchntInfo platformMchntInfo = bspsPlatformMchntInfoMapper.selectBspsPlatformMchntInfoByInstIdAndMchntNo(instId, mchntBindInfo.getMchntNo());
                        if (null == platformMchntInfo || !MchntStatusEnum.NORMAL.getStatus().equals(platformMchntInfo.getPlatMchntStatus())) {
                            rglog.error("平台商户信息查询异常或状态异常");
                        } else {
                            facadeMchntBinInfo.setMchntName(platformMchntInfo.getPlatMchntName());
                            facadeMchntBindInfoBeanList.add(facadeMchntBinInfo);
                        }
                    } else if (BspsMchntTypeEnum.GROUP_MCHNT.getMchntType().equals(mchntBindInfo.getBindMchntType())) {
                        MchntService mchntService = new MchntServiceImpl();
                        GroupMchntInfo groupMchntInfo = mchntService.getGroupMchntInfo(instId, mchntBindInfo.getMchntNo());
                        if (null == groupMchntInfo || !MchntStatusEnum.NORMAL.getStatus().equals(groupMchntInfo.getGroupMchntStatus())) {
                            rglog.error("集团商户信息查询异常或状态异常");
                        } else {
                            facadeMchntBinInfo.setMchntName(groupMchntInfo.getGroupMchntName());
                            facadeMchntBindInfoBeanList.add(facadeMchntBinInfo);
                        }
                    } else {
                        MchntBaseInfo mchntBaseInfo = bspsMchntBaseInfoMapper.selectBspsMchntBaseInfoByPrimaryKey(instId, mchntBindInfo.getMchntNo());
                        if (null == mchntBaseInfo || !MchntStatusEnum.NORMAL.getStatus().equals(mchntBaseInfo.getMchntStatus())) {
                            rglog.error("商户信息查询异常或商户状态异常");
                        } else {
                            facadeMchntBinInfo.setMchntName(mchntBaseInfo.getMchntName());
                            facadeMchntBinInfo.setCashWithdrawalFlag(mchntBaseInfo.getCashWithdrawalFlag());
                            facadeMchntBindInfoBeanList.add(facadeMchntBinInfo);
                        }
                    }
                }

                if (CollectionUtils.isEmpty(mchntBindInfoList) || CollectionUtils.isEmpty(facadeMchntBindInfoBeanList)) {
                    rglog.info("用户<{}>还未绑定任何商户!", userInfo.getUserId());
                    if (!"OPENID".equals(userInfo.getOpenId())) {
                        rglog.info("拓展经理用户查询");
                        ManagerInfo managerInfo = null;
                        BspsManagerInfoMapper bspsManagerInfoMapper = new BspsManagerInfoMapperImpl();
                        if ("APP".equals(getUserInfoListRequest.getHeader().getChanlId())){
                            managerInfo = bspsManagerInfoMapper.selectBspsManagerInfoById(instId,getUserInfoListRequest.getOpenId());
                        }else {
                            managerInfo = bspsManagerInfoMapper.selectBspsManagerInfoByOpenId(instId, userInfo.getOpenId());
                        }
                        if (null != managerInfo) {
                            rglog.info("用户已认证拓展经理，无权操作商户菜单");
                            throw new BizException(RespCodeEnum.MANAGER_VERIFY_ERROR.getRespCode(), RespCodeEnum.MANAGER_VERIFY_ERROR.getRespDesc());
                        }
                    }
                    rglog.info("同步更新用户信息，不是拓展经理也不是商户，状态修改为解绑");
                    userInfo.setLastOprId(userInfo.getUserId());
                    userInfo.setUserStatus(UserStatusEnum.CANCEL.getStatus());
                    userInfo.setLastUpdateTime(DateUtil.getCurrentDateTime(BspsAppParamConstant.yyyy_MM_dd_HHmmssSSS));

                    /* 开启数据库事务 */
                    XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
                    String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.OLTP_POOL_NAME);
                    String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
                    if (null != corporation && !corporation.isEmpty()) {
                        String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
                        dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.OLTP_POOL_NAME);
                    }
                    DbsUtil dbsUtil = new DbsUtil(dbPoolName);
                    dbsUtil.dbsBeginTransaction();

                    int iRCode = bspsUserInfoMapper.updateBspsUserInfoByPrimaryKey(userInfo);
                    if (0 != iRCode && BspsAppParamConstant.NODATA_CODE != iRCode) {
                        dbsUtil.dbsEndTransaction(false);
                        rglog.error("同步更新商户用户状态失败");
                        throw new BizException(RespCodeEnum.GET_USER_BIND_INFO_ERROR.getRespCode(), RespCodeEnum.GET_USER_BIND_INFO_ERROR.getRespDesc());
                    }
                    iRCode = bspsMchntBindInfoMapper.updateBspsMchntBindInfoStatusByUserId(instId, userInfo.getUserId());
                    if (0 != iRCode && BspsAppParamConstant.NODATA_CODE != iRCode) {
                        dbsUtil.dbsEndTransaction(false);
                        rglog.error("同步更新商户绑定状态失败");
                        throw new BizException(RespCodeEnum.GET_USER_BIND_INFO_ERROR.getRespCode(), RespCodeEnum.GET_USER_BIND_INFO_ERROR.getRespDesc());
                    }
                    dbsUtil.dbsEndTransaction(true);

                    throw new BizException(RespCodeEnum.MOBILE_USER_EXIST_ERROR.getRespCode(), RespCodeEnum.MOBILE_USER_EXIST_ERROR.getRespDesc());
                }
                getUserInfoListResponse.setMchntBindInfoBeanList(facadeMchntBindInfoBeanList);
            } else {
                rglog.info("用户<{}>还未绑定任何商户!", userInfo.getUserId());
                throw new BizException(RespCodeEnum.MOBILE_USER_EXIST_ERROR.getRespCode(), RespCodeEnum.MOBILE_USER_EXIST_ERROR.getRespDesc());
            }
            List<UserInfoBean> userInfoBeans = new ArrayList<>();
            UserInfoBean userInfoBean = new UserInfoBean();
            userInfoBean.setNickName(userInfo.getNickName());
            userInfoBean.setUserId(userInfo.getUserId());
            userInfoBean.setUserLoginId(userInfo.getUserLoginId());
            userInfoBeans.add(userInfoBean);
            getUserInfoListResponse.setUserInfoBeanList(userInfoBeans);
            registerUserLoginInfo(getUserInfoListRequest, userInfo);

            UserSessionInfo keySessionInfo = JSON.parseObject(RedisUtil.onceGet(header.getSession()), UserSessionInfo.class);
            if (null == keySessionInfo) {
                rglog.error("session超时");
                throw new BizException(RespCodeEnum.USER_NOT_LOGIN_OR_SESSION_IS_EXPIRED.getRespCode(), RespCodeEnum.USER_NOT_LOGIN_OR_SESSION_IS_EXPIRED.getRespDesc());
            }
            keySessionInfo.setUserId(userInfo.getUserId());
            RedisUtil.onceSet(header.getSession(), toJSONString(keySessionInfo), BspsAppParamConstant.SESSION_EFFECTIVE_TIME);
            //生成新的会话id
            String newSessionId = RandomUtil.getUUID();
            UserSessionInfo userSessionInfo = new UserSessionInfo();
            userSessionInfo.setUserId(userInfo.getUserId());
            userSessionInfo.setSignatureKey(keySessionInfo.getSignatureKey());
            userSessionInfo.setSensitiveKey(keySessionInfo.getSensitiveKey());
            RedisUtil.onceSet(newSessionId, toJSONString(userSessionInfo), BspsAppParamConstant.SESSION_EFFECTIVE_TIME);
            header.setSession(newSessionId);
            getUserInfoListResponse.setHeader(header);
        } else {
            /* 多个用户信息，将用户信息列表返回  */
            rglog.info("手机号对应多个用户信息");
            List<UserInfoBean> facadeUserInfoBeanList = userFilterList.stream().map(this::convertFacadeUserInfo).collect(Collectors.toList());
            getUserInfoListResponse.setUserInfoBeanList(facadeUserInfoBeanList);
        }

        /* 拼装响应结果 */
        BizResponse<GetUserInfoListResponse> bizResponse = new BizResponse<>();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(getUserInfoListResponse);
        return bizResponse;
    }

    private UserInfoBean convertFacadeUserInfo(UserInfo userInfo) {
        UserInfoBean facadeUserInfoBean = new UserInfoBean();
        facadeUserInfoBean.setUserId(userInfo.getUserLoginId());
        facadeUserInfoBean.setNickName(userInfo.getNickName());
        return facadeUserInfoBean;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        GetUserInfoListResponse getUserInfoListResponse = (GetUserInfoListResponse) bizResponse.getResult();
        ConvertUtil.convertInput(getUserInfoListResponse);
    }

    private PrivilegeInfoBean convertPrivilegeInfo(UserRoleInfo userRoleInfo) {
        PrivilegeInfoBean privilegeInfoBean = new PrivilegeInfoBean();
        BeanUtil.beanCopy(userRoleInfo, privilegeInfoBean);
        return privilegeInfoBean;
    }


    /**
     * 登记用户登录登出信息
     *
     * @param request 用户请求参数
     * @throws Exception 异常
     */
    private void registerUserLoginInfo(GetUserInfoListRequest request, UserInfo userInfo) throws Exception {
        rglog.info("用户登录日志操作开始");
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        UserLoginInfo userLoginInfo = new UserLoginInfo();
        BspsUserLoginInfoMapper bspsUserLoginInfoMapper = new BspsUserLoginInfoMapperImpl();
        userLoginInfo.setUserId(userInfo.getUserId());
        userLoginInfo.setInstId(request.getHeader().getInstId());
        userLoginInfo.setLoginType(request.getHeader().getChanlId());
        userLoginInfo.setLoginTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
        userLoginInfo.setLastOprTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
        userLoginInfo.setSessionId(request.getHeader().getSession());
        userLoginInfo.setLoginStatus("0");   //0-登录  1-登出

        userLoginInfo.setLoginIp(request.getDevRiskInfo().getDevIp());
        userLoginInfo.setDevModel(request.getDevRiskInfo().getDevModel());
        userLoginInfo.setDevSn(request.getDevRiskInfo().getDevSerialNo());
        userLoginInfo.setDevMac(request.getDevRiskInfo().getDevMac());
        userLoginInfo.setDevUnqId(request.getDevRiskInfo().getDevUniqueId());
        userLoginInfo.setLoginLongitude(request.getDevRiskInfo().getLongitude());
        userLoginInfo.setLoginLatitude(request.getDevRiskInfo().getLatitude());
        userLoginInfo.setAppVersion(request.getHeader().getVersion());
        /* 开启数据库事务 */
        String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.OLTP_POOL_NAME);
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        dbsUtil.dbsBeginTransaction();

        int iReturnCode = bspsUserLoginInfoMapper.insertBspsUserLoginInfo(userLoginInfo);
        if (0 != iReturnCode) {
            dbsUtil.dbsEndTransaction(false);
            rglog.error("登记用户登录信息表失败,iReturnCode:<{}>", iReturnCode);
            throw new BizException(RespCodeEnum.REGISTER_USER_LOGIN_INFO_ERROR.getRespCode(), RespCodeEnum.REGISTER_USER_LOGIN_INFO_ERROR.getRespDesc());
        }

        /* 更新openID */
        if (!StringUtils.isEmpty(request.getOpenId()) && "WECHAT".equals(request.getHeader().getChanlId())) {
            BspsUserInfoMapper bspsUserInfoMapper = new BspsUserInfoMapperImpl();
            iReturnCode = bspsUserInfoMapper.updateBspsOpenIdByPrimaryKey(userInfo, request.getOpenId());
            if (0 != iReturnCode) {
                dbsUtil.dbsEndTransaction(false);
                rglog.error("登记用户登录信息表失败,iReturnCode:<{}>", iReturnCode);
                throw new BizException(RespCodeEnum.UPDATE_USER_INFO_ERROR.getRespCode(), RespCodeEnum.UPDATE_USER_INFO_ERROR.getRespDesc());
            }
        }

        dbsUtil.dbsEndTransaction(true);
        rglog.info("用户登录登出日志操作结束");
    }
}
